class LevelMapGenerator {
    constructor(difficulty) {
        this.difficulty = difficulty;
        this.nodeTypes = [NodeType.ENCOUNTER, NodeType.SHOP, NodeType.BATTLE, NodeType.EVENT];
        this.depth = this.calculateDepth(difficulty);
    }

    calculateDepth(difficulty) {
        switch(difficulty) {
            case LevelDifficulty.EASY: return 3;
            case LevelDifficulty.NORMAL: return 4;
            case LevelDifficulty.HARD: return 5;
            case LevelDifficulty.EXPERT: return 6;
            default: return 4;
        }
    }

    generateMap() {
        const root = this.createNode(0);
        root.x = 0;
        root.y = 0;
        
        // 创建每一层节点，包括起始节点层
        const layers = [[root]];  // 修改这里，将root放入第一层
        
        // 生成中间层
        for (let level = 1; level < this.depth - 1; level++) {
            const layer = [];
            const nodeCount = this.getNodeCountForLayer(level);
            
            for (let i = 0; i < nodeCount; i++) {
                const node = this.createNode(level);
                node.y = level * 120;
                node.x = (i - (nodeCount-1)/2) * 150;
                layer.push(node);
            }
            
            layers.push(layer);
        }
        
        // 创建终点节点（BOSS节点）
        const finalNode = this.createNode(this.depth - 1, true);
        finalNode.y = (this.depth - 1) * 120;
        finalNode.x = 0;
        layers.push([finalNode]);
        
        // 连接节点
        this.connectNodes(layers);
        
        return root;
    }

    getNodeCountForLayer(level) {
        if (level === 0 || level === this.depth - 1) return 1;
        return Math.min(3 + Math.floor(this.difficulty/2), 5);
    }

    connectNodes(layers) {
        for (let i = 0; i < layers.length - 1; i++) {
            const currentLayer = layers[i];
            const nextLayer = layers[i + 1];
            
            // 确保每个节点至少有一个连接
            currentLayer.forEach(node => {
                const connections = 1 + Math.floor(Math.random() * 2); // 1-2个连接
                const availableTargets = [...nextLayer]; // 创建可用目标节点的副本
                
                for (let j = 0; j < Math.min(connections, availableTargets.length); j++) {
                    const targetIndex = Math.floor(Math.random() * availableTargets.length);
                    const targetNode = availableTargets.splice(targetIndex, 1)[0]; // 移除并获取目标节点
                    node.addChild(targetNode);
                }
            });
            
            // 确保下一层的每个节点至少有一个父节点
            nextLayer.forEach(node => {
                if (!this.hasParent(node, currentLayer)) {
                    const sourceIndex = Math.floor(Math.random() * currentLayer.length);
                    currentLayer[sourceIndex].addChild(node);
                }
            });
        }
    }

    hasParent(node, parentLayer) {
        return parentLayer.some(parent => parent.children.includes(node));
    }

    createNode(level, isBoss = false) {
        if (isBoss) {
            return new LevelNode('boss', this.difficulty, {
                name: '层级BOSS',
                type: NodeType.BATTLE,
                difficulty: this.difficulty
            });
        }
        
        const type = this.nodeTypes[Math.floor(Math.random() * this.nodeTypes.length)];
        const nodeDifficulty = Math.min(
            this.difficulty,
            Math.ceil(this.difficulty * (level + 1) / this.depth)
        );
        
        return new LevelNode(type, nodeDifficulty, 
            LevelContentGenerator.generateContent(type, nodeDifficulty));
    }
} 